package database.helpers;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class Testimony_Table {

public databas_helper dbHelper;	
public SQLiteDatabase database;
	/**
	 * Assignment table related constants.
	 */
	public final static String TESTIMONY_TABLE="Testimony";	
	public final static String TESTIMONY_TYPE="TYPE";
	public final static String TESTIMONY_USERNAME="USERNAME";	
	public final static String TESTIMONY_URL="URL";
	public final static String TESTIMONY_CONTENT="CONTENT";
	public final static String TESTIMONY_ID="_id";
	
	public Testimony_Table(Context context){
		dbHelper = new databas_helper(context);
		database = dbHelper.getWritableDatabase();
	}
	public Testimony_Table(databas_helper dbHelper,SQLiteDatabase database){
		this.dbHelper = dbHelper;
		this.database = database;
	}
	
	/**\
	 * Creates a new Testimony
	 	 */
	public long addTestimony(String TYPE,String USERNAME, String URL, String CONTENT){
		ContentValues values = new ContentValues();
		values.put(TESTIMONY_TYPE,TYPE);
		values.put(TESTIMONY_USERNAME,USERNAME);
		values.put(TESTIMONY_URL,URL);
		values.put(TESTIMONY_CONTENT,CONTENT);

		return database.insert(TESTIMONY_TABLE, null, values);
	}	
	
	public void close() {
		dbHelper.close();
	}
	/**
	 * Fetch all Testimonys
	 * @return
	 */
	public Cursor getAllTestimonies(){
		Cursor mCursor = database.query(true, TESTIMONY_TABLE, new String[]
				{
				TESTIMONY_ID,TESTIMONY_TYPE,TESTIMONY_USERNAME, TESTIMONY_URL, TESTIMONY_CONTENT},null
				, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}
	/**
	 * 
	 */
	public int editTestimony(String TYPE,String USERNAME, String URL, String CONTENT){
		ContentValues values = new ContentValues();
		values.put(TESTIMONY_TYPE,TYPE);
		values.put(TESTIMONY_USERNAME,USERNAME);
		values.put(TESTIMONY_URL,URL);
		values.put(TESTIMONY_CONTENT,CONTENT);
		
		return database.update(TESTIMONY_TABLE, values,TESTIMONY_ID + "=?",new String[]{TYPE});
	}
	
	public boolean delete(String TYPE){
		return database.delete(TESTIMONY_TABLE, TESTIMONY_ID + "=" + TYPE, null) > 0;
	}
}
